package com.cqwshzj.framework.service.impl;/**
 * @Description:
 * @Auther: Huang Zhijin
 * @Version: 1.0.0
 * @Date: 2021/4/7 09:15
 */

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.cqwshzj.common.JobConstant;
import com.cqwshzj.common.column.JobStrategyColumn;
import com.cqwshzj.framework.dao.api.JobStrategyDao;
import com.cqwshzj.framework.dao.model.JobStrategy;
import com.cqwshzj.framework.service.JobStrategyService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 *
 * 〈〉
 *
 * @author Huang Zhijin
 * @create 2021/4/7
 * @since 1.0.0
 */
@Service
public class JobStrategyServiceImpl implements JobStrategyService {

	public static final Logger log = LoggerFactory.getLogger(JobStrategyServiceImpl.class);

	@Autowired
	private JobStrategyDao dao;

	@Override
	public JobStrategy getById(Integer id) {
		if (id == null) {
			return null;
		}
		return dao.selectById(id);
	}

	@Override
	public List<JobStrategy> listEnableStrategyOfToday() {
		final String today = LocalDate.now().format(DateTimeFormatter.ofPattern(JobConstant.DATE_1));

		QueryWrapper<JobStrategy> wrapper = new QueryWrapper<>();
		wrapper.eq( JobStrategyColumn.NEXT_RUN_DATE, today );
		wrapper.eq( JobStrategyColumn.STATE, JobConstant.STATUS_ENABLE );
		wrapper.isNotNull( JobStrategyColumn.JOB_TYPE );
		wrapper.isNotNull( JobStrategyColumn.STRT_TYPE );
		return dao.selectList( wrapper );
	}

	@Transactional( propagation = Propagation.REQUIRED, rollbackFor = Throwable.class )
	@Override
	public int lockAndUpdateStateDisabledById(Integer id, String nextRunDate) {
		Assert.notNull(id,"缺少参数 id");
		Assert.hasText(nextRunDate,"缺少参数 nextRunDate");

		JobStrategy jobStrategy = dao.selectOneEnabledForUpdateByIdAndNextRunDate(id, nextRunDate);

		Assert.notNull( jobStrategy,"没有找到 "+id+" 对应的任务策略");
		Assert.isTrue( JobConstant.STATUS_ENABLE.equals( jobStrategy.getState() ) ,id+" 对应的任务策略已加锁");

		UpdateWrapper<JobStrategy> wrapper = new UpdateWrapper<>();
		wrapper.eq(JobStrategyColumn.ID, id);

		JobStrategy entity = new JobStrategy();
		entity.setState( JobConstant.STATUS_DISABLE );
		return dao.update( entity, wrapper );
	}

	@Override
	public int updateStatusEnableById(Integer id) {
		Assert.notNull(id,"缺少参数 id");

		UpdateWrapper<JobStrategy> wrapper = new UpdateWrapper<>();
		wrapper.eq(JobStrategyColumn.ID, id);

		JobStrategy entity = new JobStrategy();
		entity.setState( JobConstant.STATUS_ENABLE );
		return dao.update(entity, wrapper);
	}


	@Override
	public int updateNextRunDateAndStatuaEnableById(Integer id, String nextRunDate) {
		Assert.notNull(id,"缺少参数 id");
		Assert.hasText(nextRunDate,"缺少参数 nextRunDate");

		UpdateWrapper<JobStrategy> wrapper = new UpdateWrapper<>();
		wrapper.eq( JobStrategyColumn.ID , id );

		JobStrategy entity = new JobStrategy();
		entity.setNextRunDate( nextRunDate );
		entity.setState( JobConstant.STATUS_ENABLE );

		return dao.update(entity, wrapper);
	}
}
